<?php 

class MY_Controller extends CI_Controller 
{
	public $user_map = '';
	public function __construct()
	{
		parent::__construct();
		$this->openid = isset($_GET['open_id']) ? $_GET['open_id'] : $this->input->cookie("openid");
		if(isset($_GET['open_id']))
		{
			header("Location:" . base_url('api/setUserStatus?open_id=' . $_GET['open_id']));
			exit();
		}
		if($this->openid)
		{
			$userinfo = $this->getUserInfo($this->openid);
			/*if(!empty($userinfo->data->mobi))
			{
				$this->updateFirstVisitState();	
			}*/
			$this->checkOpenid();
			
			if(empty($userinfo->data->mobi))			//用户未绑定手机号则跳转到中转页面
			{
				$this->UpdateUnbindTime();
				header("Location:http://mp.weixin.qq.com/s?__biz=MzA4NDE0OTYzNA==&mid=243172973&idx=1&sn=7715e242da7e76f20de56d3e8a44e446#rd");
				exit();

			}
			
			$this->input->set_cookie("openid", $this->openid, 3600 * 24 * 900);
		}
		else
		{
			$this->is_attention = false;
			$router = $this->router->fetch_class() . '_' . $this->router->fetch_method();
			if(('home_index' != $router))
			{
				header("Location:http://mp.weixin.qq.com/s?__biz=MzA4NDE0OTYzNA==&mid=239739583&idx=1&sn=50fd20c7ca5a339c7228f0452b4f6450#rd");
				exit();
			}
		}
		//$this->updateFirstVisitState();

			parse_str($_SERVER['QUERY_STRING'], $arr);
			unset($arr['refer']);
			unset($arr['sid']);
			$tp_str = http_build_query($arr);
			$result = $_SERVER['REDIRECT_URL'];
			if(!empty($arr))
			{
				$result = $_SERVER['REDIRECT_URL'] .'?'. $tp_str;
			}

		$this->user_map = "refer=".base64_encode($result) . '&sid='.md5($this->config->item('user_key').$this->openid);
	}

	public function insertVisitUserinfo($type="")
	{
		$ctime=time();
		if($this->openid)
		{
			$save_data = array(
			'openid' => $this->openid,
			'type' => $type,
			'user_agent' => $_SERVER["HTTP_USER_AGENT"],
			'remote_ip' => $_SERVER["REMOTE_ADDR"],
			'request_data' => serialize($_REQUEST),
			'ctime' =>$ctime
			);
			$this->load->model('user_visit_model');
			$this->user_visit_model->insertUserData($save_data);
		}
	}

	//获取当前时间戳精确到毫秒13位数字
	public function getMillisecond() 		
	{
		list($t1, $t2) = explode(' ', microtime());     
		$this->jointime = (float)sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);  
	}

	public function checkOpenid()
	{
		$this->getMillisecond();
		$this->load->model('user_model');
		$area = isset($_GET['from']) ? $_GET['from'] : '';
		$logintime = date('Y-m-d H:i:s',time());
		$is_first = 0;	//0为第一次访问
		$data = array(
			'name' => '',
			'openid' => $this->openid,
			'hopenid' =>'',
			'area' => $area,
			'is_first' => $is_first,
			'in_time' => $logintime,				//第一次访问时间
			'update_time' => $logintime,			//最后一次访问时间
			'join_time' => $this->jointime
		);
		if($this->openid)
		{
			$this->user_model->insert_user($data);
		}

	}

	//更新是否第一次访问的状态
	public function updateFirstVisitState()
	{
		$this->load->model('user_model');
		$this->user_model->updateFirstState($this->openid);

	}
	public function UpdateUnbindTime()
	{
		$this->load->model('user_model');
		$result = $this->user_model->getUserByOpenId($this->openid);
		if($result->unbind_time == 0)
		{
			 $this->user_model->UpdateUnbindtime($this->openid);
		}
	}

	public function getUserInfo($openid)
	{

		/*$this->load->library('memcache_client');
		$cache_key = md5('user_info_weili_' . $openid);
		if($user_row = $this->memcache_client->get($cache_key))
		{
			return $user_row;
		}*/
		$this->load->library('weili');
		$return_data = $this->weili->getUserInfoByOpenId($openid);
		$temp_data_obj = json_decode($return_data);
		if(!$temp_data_obj->code && $temp_data_obj->data->mobi !='')
		{
		 	//手机号不为空缓存
		 	//$this->memcache_client->add($cache_key, $temp_data_obj, 172800);
		 	
		}
		return $temp_data_obj;
	
	}
}